package com.example.myapp.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapp.CommentListAdapter;
import com.example.myapp.R;

import com.example.myapp.RegistActivity;
import com.example.myapp.entity.CommentInfo;
import com.example.myapp.entity.UserInfo;

import java.util.ArrayList;
import java.util.List;

import cn.leancloud.LCObject;
import cn.leancloud.LCQuery;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MessageFragment extends Fragment {

    private View rootView;
    private EditText et_content;
    private RecyclerView recyclerView;
    private CommentListAdapter commentAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_message, container, false);

        //初始化控件
        et_content = rootView.findViewById(R.id.et_content);
        recyclerView = rootView.findViewById(R.id.recyclerView);

        //初始化适配器
        commentAdapter = new CommentListAdapter();
        //设置适配器
        recyclerView.setAdapter(commentAdapter);
        
        rootView.findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = et_content.getText().toString();
                if(TextUtils.isEmpty(content)){
                    Toast.makeText(rootView.getContext(), "留言内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                //获取用户名
                UserInfo userInfo = UserInfo.getUserInfo();
                String username = userInfo.getUsername();
                // 构建对象
                LCObject todo = new LCObject("CommentTable");
                // 为属性赋值
                todo.put("username", username);
                todo.put("comment", content);
                // 将对象保存到云端
                todo.saveInBackground().subscribe(new Observer<LCObject>() {
                    public void onSubscribe(Disposable disposable) {}
                    public void onNext(LCObject todo) {
                        // 成功保存之后，执行其他逻辑
                        Toast.makeText(rootView.getContext(), "留言成功", Toast.LENGTH_SHORT).show();
                        //清空输入框内容
                        et_content.setText("");
                        //刷新列表
                        initData();
                    }
                    public void onError(Throwable throwable) {
                        // 异常处理
                    }
                    public void onComplete() {}
                });

            }
        });

        //获取留言
        initData();

        return rootView;
    }


    private void initData(){

        LCQuery<LCObject> query = new LCQuery<>("CommentTable");
        query.whereNotEqualTo("username", null);
        query.findInBackground().subscribe(new Observer<List<LCObject>>() {
            public void onSubscribe(Disposable disposable) {}
            public void onNext(List<LCObject> Comments) {
                List<CommentInfo> commentInfoList = new ArrayList<>();
                for(LCObject LCcommentInfo :Comments){
                    CommentInfo commentInfo = new CommentInfo(LCcommentInfo.getString("username"),LCcommentInfo.getString("comment"));
                    commentInfoList.add(commentInfo);
                }
                commentAdapter.setCommentInfoList(commentInfoList);
            }
            public void onError(Throwable throwable) {}
            public void onComplete() {}
        });


    }
}